﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
using System;

namespace Bootstrap.Security.Mvc
{
    /// <summary>
    /// BootstrapAdmin 应用程序上下文
    /// </summary>
    public static class BootstrapAppContext
    {
#nullable disable
        /// <summary>
        /// 获得 当前应用程序配置 appId 值
        /// </summary>
        public static string AppId { get; private set; }

        /// <summary>
        /// 获得 当前应用程序 IConfiguration 实例
        /// </summary>
        public static IConfiguration Configuration { get; private set; }

        /// <summary>
        /// 获得 当前应用程序容器服务接口实例
        /// </summary>
        public static IServiceProvider ServiceProvider { get; private set; }
#nullable restore

        /// <summary>
        /// 设置当前上下文 IConfiguration 实例
        /// </summary>
        /// <param name="configuration"></param>
        internal static void SetConfiguration(IConfiguration configuration)
        {
            Configuration = configuration;
            AppId = configuration.GetValue("AppId", "");

            ChangeToken.OnChange(() => Configuration.GetReloadToken(), () => AppId = configuration.GetValue("AppId", ""));
        }

        /// <summary>
        /// 设置 当前上下文容器服务器接口实例
        /// </summary>
        /// <param name="provider"></param>
        internal static void SetProvider(IServiceProvider provider) => ServiceProvider = provider;
    }
}
